package com.aaa.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.entity.MyReturn;
import com.aaa.entity.Tester;
import com.aaa.service.TestInsertService;
import com.aaa.service.TesterSASService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("testInsert")
public class TestInserController extends BaseController{
    @Autowired
    private TestInsertService testInsertService;

    MyReturn myReturn=new MyReturn();

    @Autowired
    private TesterSASService testerSASService;

    /**
     * 测试总分数
     */
    private int sum=0;
    /**
     * 正常临界值
     */
    private static final int NORMAL=50;
    /**
     * 轻度临界值
     */
    private static final int MILD=59;
    /**
     * 中度临界值
     */
    private static final int MODERATE=69;
    
    /**
    * @Author: WTH
    * @Description: SAS测试答题结果及程度判断
    * @Date:2022-07-08 18:52
    * @Params:  * @param null
    * @return: 
    */
    @ResponseBody
    @RequestMapping("insertSasTest.do")
    public MyReturn insertSASTest(String res, HttpSession session){
        List<Map> maps= JSON.parseArray(res,Map.class);
        Tester tester= (Tester) session.getAttribute("tester");
        testerSASService.addTester(tester);
        for(Map map:maps){
            map.put("tester_id",tester.getId());
        }
        boolean b = testInsertService.testInsertSas(maps);
        List<Tester> testers = testerSASService.selectResultOne(tester.getId());
        Tester tester1 = testers.get(0);
        if(ObjectUtil.isNotEmpty(tester1)){
            sum = tester1.getFordword_scoring()+tester1.getReverse_scoring();
        }

        if(sum<NORMAL){
            myReturn.setData("/views/character/SasNormal.jsp");
        }else if(sum>=NORMAL && sum<=MILD){
            myReturn.setData("/views/character/SasMild.jsp");
        }else if(sum>MILD && sum<=MODERATE){
            myReturn.setData("/views/character/SasModerate.jsp");
        }else if(sum>MODERATE){
            myReturn.setData("/views/character/SasSevere.jsp");
        }
        if(b){
            return super.getSuccessResult(myReturn);
        }else{
            return super.getFailResult(myReturn);
        }
    }
}
